# 类的初始化、继承、覆盖、封装
# car类
class Car():
    def __init__(self,make,model,year):
        self.make=make
        self.model=model
        self.year=year
        self.odometer_reading=0#该属性的初始值为0   #里程表


    def get_descriptive_name(self):
        long_name=str(self.year)+' '+self.make+'  '+self.model
        return  long_name.title()
    def read_odometer(self):
        print('this car has  '+str(self.odometer_reading)+'  miles on it.')

    def update_odometer(self,mileage):
        if mileage>=self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("you cannot  roll back  an odometer ")

    def increment_odomeyer(self,miles):
        self.odometer_reading+=miles


my_new_car=Car('audi','a4',2016)
print(my_new_car.get_descriptive_name())#2016 Audi  A4
my_new_car.read_odometer()#this car has  0 miles on it.

#直接修改属性的值
my_new_car.odometer_reading=222
my_new_car.read_odometer()
#通过方法修改属性的值
my_new_car.update_odometer(333)
my_new_car.read_odometer()
# 通过方法对属性的值进行递增
my_new_car.increment_odomeyer(100)
my_new_car.read_odometer()


#将实例用作属性，将Battery实例用作ElectricCar类的一个属性
class Battery():
    def __init__(self,battery_size=70):
        self.battery_size=battery_size
    def describe_battery(self):
        print('this  Electric  car has a  ' +str(self.battery_size)+  'KWH battery ')
    def get_range(self):#电瓶的续航里程
        range=0
        if self.battery_size==70:
            range=240
        elif self.battery_size==85:
            range=270

        message='this car can go approixmately '+str(range)+'miles on a full charge'
        print(message)


#继承
class ElectricCar(Car):
    def __init__(self,make,model,year):
        super().__init__(make,model,year)
        #新属性
        # self.battery_size=70
        self.battery=Battery()#ElectricCar的新属性

    #新方法
    def describe_battery(self):
        print('this  Electric  car has a  ' +str(self.battery)+  'KWH battery ')

    #覆盖  重写父类的方法
    def fill_gas_tank(self):
        print('this car doesnot need a gas tank ')



#实例化一个子类my_tesla
my_tesla=ElectricCar('tesla','model S',2016)
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()
my_tesla.fill_gas_tank()

my_tesla.battery.describe_battery()
my_tesla.battery.get_range()